<template>
  <div class="app-container calendar-list-container">
    <div class="filter-container">
      <el-button class="filter-item" @click="handleAdd()" type="primary" icon="el-icon-plus">添加</el-button>
      <el-input class="filter-item" @keyup.enter.native="handleFilter" style="width: 200px;" placeholder="请输入" v-model="search.keyword"></el-input>
      <el-button class="filter-item" v-waves icon="el-icon-search" @click="handleFilter">搜索</el-button>
    </div>
      <el-table
         v-loading="!pload"
         @selection-change = "selectionChange"
          ref="multipleTable"
          border
          :data="tableData"
          >
          <el-table-column
            type="selection"
            width="55"
            align="center"
          >
          </el-table-column>
          <el-table-column
            show-overflow-tooltip
            property="roleId"
            label="Id"
            width="100"
            align="center"
          >
          </el-table-column>
          <el-table-column
            show-overflow-tooltip
            property="roleName"
            label="名称"
            align="center"
            >
          </el-table-column>
          <el-table-column
            show-overflow-tooltip
            property="roleCode"
            label="角色码"
            align="center"
            width="100"
            >
          </el-table-column>
          <el-table-column
            show-overflow-tooltip
            property="comment"
            label="备注"
            >
          </el-table-column>
          <el-table-column
            property="status"
            label="状态"
            align="center"
            width="100"
            >
            <template slot-scope="scope">
              <el-tag v-if="!scope.row.roleStatus" type="success" size="mini">启用</el-tag>
              <el-tag v-else type="danger" size="mini">停用</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="分配" width="200" align="center">
            <template slot-scope="scope">
              <el-button size="mini" @click="handleAccount(scope.$index, scope.row)">帐号分配</el-button>
              <el-button size="mini" @click="handlePower(scope.$index, scope.row)">功能分配</el-button>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="200" align="center">
            <template slot-scope="scope">
              <el-button size="mini" type="primary" @click="handleEdit(scope.$index, scope.row)">修改</el-button>
              <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
            </template>
          </el-table-column>
      </el-table>
    <div class="filter-bottom">
      <el-button-group class="filter-item">
        <el-button @click="toggleSelection(-1)">全选</el-button>
        <el-button @click="toggleSelection(0)" :disabled="!selection.length">反向选择</el-button>
        <el-button @click="toggleSelection(1)" :disabled="!selection.length">取消选择</el-button>
        <el-button @click="toggleDel" :disabled="!selection.length">删除</el-button>
      </el-button-group>
    </div>
    <div class="pagination-container">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
      :page-size="limit"
      :page-sizes="limits"
      background
      layout="total, sizes, prev, pager, next, jumper"
      :total="count">
      </el-pagination>
    </div>
    <edit v-if="edit" :data="selectTable" @getData="getData"></edit>
    <roleAssignAccount v-if="roleAssignAccount" :data="selectTable" @getData="getData"></roleAssignAccount>
    <roleAssignPower v-if="roleAssignPower" :data="selectTable" @getData="getData"></roleAssignPower>
  </div>
</template>

<script>
    import {roleList,roleRemove} from '@/utils/api'
    import waves from '@/directive/waves' // 水波纹指令
    import edit from './edit'
    import roleAssignAccount from './assignAccount'
    import roleAssignPower from './assignPower'
    export default {
      directives: {
        waves
      },
      data(){
          return {
            tableData: [],
            selection: [],
            selectTable: {},
            search: {
              keyword: ''
            },
            pload: false,
            edit: false,
            roleAssignAccount: false,
            roleAssignPower: false,
            currentPage:1,
            limit: 20,
            limits: [10,20,50,100],
            count:0,
          }
      },
      components: {
        edit,
        roleAssignAccount,
        roleAssignPower
      },
      created(){
         this.getData()
      },
      methods: {
        selectionChange (selection) {
          this.selection = selection
        },
        toggleSelection(s) {
          if (s===-1) {
            this.tableData.forEach(row => {
              this.$refs.multipleTable.toggleRowSelection(row,true)
            })
          }
          if (s===0) {
            this.tableData.forEach(row => {
              this.$refs.multipleTable.toggleRowSelection(row)
            })
          }
          if (s===1) {
            this.$refs.multipleTable.clearSelection()
          }
        },
        handleAdd () {
          this.selectTable = {}
          this.selectTable.method = 'add'
          this.edit = true
        },
        async handleDelete(index, row) {
          this.$confirm('确认要删除吗',{type:'warning'}).then(async () => {
            try{
              this.pload = false;
              await roleRemove({roleIds: row.roleId})
              this.$notify({title: '删除成功', type: 'success', duration: 5000})
              this.getData()
            } catch(e) {
              this.pload = true;
            }
          })
        },
        toggleDel () {
          this.$confirm('确认要删除吗',{type:'warning'}).then(async () => {
            let ids = this.selection.map((i,index) => {
              return i.roleId
            }).join(',')
            try{
              this.pload =false;
              await roleRemove({roleIds: ids})
              this.$notify({title: '删除成功', type: 'success', duration: 5000})
              this.getData()
            } catch(e) {
              this.pload = true;
            }
          })
        },
        handleEdit(index, row) {
          this.selectTable = JSON.parse(JSON.stringify(row))
          this.selectTable.method = 'edit'
          this.edit = true
        },
        handleAccount(index, row) {
          this.selectTable = JSON.parse(JSON.stringify(row))
          this.roleAssignAccount = true
        },
        handlePower(index, row) {
          this.selectTable = JSON.parse(JSON.stringify(row))
          this.roleAssignPower = true
        },
        async getData(){
          this.pload = false
          const Datas = await roleList({
            pageNum: this.currentPage,
            pageSize: this.limit,
            keyword: this.search.keyword
          })
          console.log(Datas.data)
          this.tableData = Datas.data
          this.count = Datas.pageInfo.total
          this.pload = true
        },
        handleSizeChange(val) {
          console.log(`每页 ${val} 条`)
          this.limit = val
          this.getData()
        },
        handleCurrentChange(val) {
          this.currentPage = val
          this.getData()
        },
        handleFilter () {
          this.getData()
        }
      }
    }
</script>